package com.mycompany.user.service;

import com.mycompany.mvc.utils.BaseResult;
import com.mycompany.mvc.utils.SystemConstant;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.user.dao.PermissionDao;
import com.mycompany.user.dao.UserDao;
import com.mycompany.user.entity.Permission;
import com.mycompany.user.entity.User;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;

	@Autowired
	private PermissionDao permissionDao;

	@Override
	public ModelAndView login(User user) {
		ModelAndView mView = new ModelAndView();
		Map<String,Object> existMap=new HashMap<String,Object>();
		existMap.put("name",user.getName());
		existMap.put("identity",1);
		User userExist = userDao.selectByParam(existMap);
		if (SystemUtil.isNull(userExist)){
			mView.setViewName("/system/login");
			mView.addObject("msg","用户不存在!");
			return mView;
		}

		Map<String,Object> infoMap=new HashMap<String,Object>();
		infoMap.put("name",user.getName());
		infoMap.put("password",user.getPassword());
		infoMap.put("identity",1);
		User userInfo = userDao.selectByParam(infoMap);
		if (SystemUtil.isNull(userInfo)){
			/*重新进入登陆界面*/
			mView.setViewName("/system/login");
			mView.addObject("msg","密码错误!");
			return mView;
		}

		mView.addObject("userInfo",userInfo);
		mView.addObject("msg","欢迎:"+userInfo.getName());
		mView.setViewName("redirect:/main");
		return mView;
	}

	@Override
	public ModelAndView getMain(User user) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/system/main");
		/*添加主界面需要的信息,用户名*/
		mv.addObject("name",user.getName());
		return mv;
	}

	@Override
	/**
	 * 当前所有菜单和子菜单的集合
	 */
	public List<Permission> getAllPermission(Map<String, Object> map) {
		List<Permission> permissionList = permissionDao.selectAllByParam(map);
		System.out.println(permissionList);
		return permissionList;
	}

	@Override
	public BaseResult loginApp(User user) {
		BaseResult baseResult = new BaseResult();

		BaseResult checkResult = checkUser(user);
		if(!checkResult.isSuccess()){
			baseResult.setSuccess(false);
			baseResult.setCode(SystemConstant.returnParamError);
			baseResult.setMsg(SystemConstant.returnParamErrorMsg+":"+checkResult.getMsg());
		}

		try {
			Map<String,Object> validMap=new HashMap<String,Object>();
			validMap.put("name",user.getName());
			validMap.put("password",user.getPassword());
			validMap.put("identity",0);
			User userValid = userDao.selectByParam(validMap);

			if (SystemUtil.isNull(userValid)){
				baseResult.setSuccess(false);
				baseResult.setCode(SystemConstant.returnFal);
				baseResult.setMsg(SystemConstant.returnFalMsg+":"+"用户/密码错误");
			}else {
				baseResult.setSuccess(true);
				baseResult.setCode(SystemConstant.returnSuc);
				baseResult.setMsg(SystemConstant.returnSucMsg);
			}

		} catch (Exception e) {
		    baseResult.setSuccess(false);
		    baseResult.setCode(SystemConstant.returnFal);
		    baseResult.setMsg(SystemConstant.returnFalMsg+":系统异常-"+e.getMessage());
		    e.printStackTrace();
		}

		return baseResult;
	}

	@Override
	public BaseResult register(User user) {
		BaseResult baseResult = new BaseResult();

		Map<String,Object> map=new HashMap<String,Object>();
		try {
			Map<String,Object> existMap=new HashMap<String,Object>();
			existMap.put("name",user.getName());
			List<User> existUserList=userDao.selectAllByParam(existMap);
			if (CollectionUtils.isNotEmpty(existUserList)){
				baseResult.setSuccess(false);
				baseResult.setCode(SystemConstant.returnFal);
				baseResult.setMsg(SystemConstant.returnFalMsg+":"+"用户已存在");
				return  baseResult;
			}
			/*设置一下返回值之类*/
			user.setIdentity(0);
			userDao.insertUser(user);
			baseResult.setSuccess(true);
			baseResult.setCode(SystemConstant.returnSuc);
			baseResult.setMsg(SystemConstant.returnSucMsg);
		}catch (Exception e){
			baseResult.setSuccess(false);
			baseResult.setCode(SystemConstant.returnFal);
			baseResult.setMsg(SystemConstant.returnFalMsg+":系统异常-"+e.getMessage());
			e.printStackTrace();
		}
		return baseResult;
	}

	private BaseResult checkUser(User user){
		BaseResult baseResult = new BaseResult();
		baseResult.setSuccess(true);
		if(StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getPassword())){
			baseResult.setSuccess(false);
			baseResult.setMsg("用户名/密码不能为空");
		}
		return  baseResult;
	}

}
